class CheckInRecord {
  final int? id;
  final int date; // 签到日期（时间戳）
  final int studyTime; // 学习时长（分钟）
  final int wordsLearned; // 学习单词数
  final int questionsAnswered; // 答题数量
  final String note; // 备注
  final int createdAt;
  final Map<int, int> goalProgress; // 目标进度 <目标ID, 进度值>

  CheckInRecord({
    this.id,
    required this.date,
    this.studyTime = 0,
    this.wordsLearned = 0,
    this.questionsAnswered = 0,
    this.note = '',
    required this.createdAt,
    this.goalProgress = const {},
  });

  // 从Map创建CheckInRecord对象
  factory CheckInRecord.fromMap(Map<String, dynamic> map) {
    // 处理目标进度数据
    Map<int, int> goalProgress = {};
    if (map['goal_progress'] != null) {
      try {
        final String progressStr = map['goal_progress'] as String;
        final List<String> pairs = progressStr.split(',');
        for (final pair in pairs) {
          if (pair.isEmpty) continue;
          final parts = pair.split(':');
          if (parts.length == 2) {
            final goalId = int.tryParse(parts[0]);
            final progress = int.tryParse(parts[1]);
            if (goalId != null && progress != null) {
              goalProgress[goalId] = progress;
            }
          }
        }
      } catch (e) {
        print('解析目标进度数据出错: $e');
      }
    }
    
    return CheckInRecord(
      id: map['id']?.toInt(),
      date: map['date']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
      studyTime: map['study_time']?.toInt() ?? 0,
      wordsLearned: map['words_learned']?.toInt() ?? 0,
      questionsAnswered: map['questions_answered']?.toInt() ?? 0,
      note: map['note'] ?? '',
      createdAt: map['created_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
      goalProgress: goalProgress,
    );
  }

  // 转换为Map
  Map<String, dynamic> toMap() {
    // 将目标进度转换为字符串存储
    String goalProgressStr = '';
    if (goalProgress.isNotEmpty) {
      goalProgressStr = goalProgress.entries
          .map((entry) => '${entry.key}:${entry.value}')
          .join(',');
    }
    
    return {
      'id': id,
      'date': date,
      'study_time': studyTime,
      'words_learned': wordsLearned,
      'questions_answered': questionsAnswered,
      'note': note,
      'created_at': createdAt,
      'goal_progress': goalProgressStr,
    };
  }

  // 复制并修改
  CheckInRecord copyWith({
    int? id,
    int? date,
    int? studyTime,
    int? wordsLearned,
    int? questionsAnswered,
    String? note,
    int? createdAt,
    Map<int, int>? goalProgress,
  }) {
    return CheckInRecord(
      id: id ?? this.id,
      date: date ?? this.date,
      studyTime: studyTime ?? this.studyTime,
      wordsLearned: wordsLearned ?? this.wordsLearned,
      questionsAnswered: questionsAnswered ?? this.questionsAnswered,
      note: note ?? this.note,
      createdAt: createdAt ?? this.createdAt,
      goalProgress: goalProgress ?? Map.from(this.goalProgress),
    );
  }

  @override
  String toString() {
    return 'CheckInRecord{id: $id, date: $date, studyTime: $studyTime, goalProgress: $goalProgress}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is CheckInRecord && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}